package com.os.meituan.controller.shop;


import com.github.pagehelper.PageInfo;
import com.os.meituan.common.*;
import com.os.meituan.mapper.Shop.T_SHOP_InformationMapper;
import com.os.meituan.model.dtomodel.sec.DTO_SEC_Login;
import com.os.meituan.model.pomodel.Shop.T_SHOP_Imformation;
import com.os.meituan.model.pomodel.sec.SEC_User;
import com.os.meituan.model.pomodel.sec.T_SYS_Log;
import com.os.meituan.model.vomodel.User.VO_SEC_User_Ex;
import com.os.meituan.repository.Shop.OA_SHOP_InformationResponsitory;
import com.os.meituan.repository.User.EPLC_SEC_UserRepository;
import com.os.meituan.repository.User.EPLC_SEC_UserResponsitory;
import com.os.meituan.service.sec.CommonAuthService;
import com.os.meituan.service.sec.EPLC_SEC_UserService;
import com.os.meituan.service.sec.SYS_LogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.Page;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.persistence.criteria.Predicate;
import java.util.ArrayList;

@Api(tags = "OA-EQM-美团")
@RestController
@RequestMapping("/api/oa/fra/org")
public class ShopController {
    @Resource
    private OA_SHOP_InformationResponsitory oa_shop_informationResponsitory;

    @Resource
    private PubClass pubClass;

    @Resource
    private T_SHOP_InformationMapper t_shop_informationMapper;

    @Resource
    private EPLC_SEC_UserResponsitory eplc_sec_userResponsitory;
    @Resource
    private EPLC_SEC_UserRepository eplc_sec_userRepository;
    @Resource
    private EPLC_SEC_UserService eplc_sec_userService;

    @Resource
    private CommonAuthService commonAuthService;
    @Resource
    private SYS_LogService sys_logService;


    @ApiOperation("保存功能")
    @PostMapping("/save")
    public XJsonInfo save(@RequestBody T_SHOP_Imformation info){

      oa_shop_informationResponsitory.save(info);
      return new XJsonInfo();
    }




    @GetMapping("/getModel1")
    public XJsonInfo<VO_SEC_User_Ex> getModel1(String id) {
        var info = eplc_sec_userResponsitory.findById(id).orElse(null);
        var voInfo = JsonClass.fromJSON(JsonClass.toJSON(info), VO_SEC_User_Ex.class);

        return new XJsonInfo().setData(voInfo);
    }
    @ApiOperation("删除功能")
    @PostMapping("/delete")
    public XJsonInfo delete(@RequestBody DTO_COMMON_Ids info){
        var arrIds =StringClass.toStringList(info.getIds());
        oa_shop_informationResponsitory.deleteAllById(arrIds);
        return new XJsonInfo();
    }
    @ApiOperation("单个查询功能")
    @GetMapping("/getModel")
    public XJsonInfo getModel(String id){
        if (StringClass.isNullOrEmpty(id)) {
            return new XJsonInfo(false, "id不能为空");
        }
      var model=  oa_shop_informationResponsitory.findById(id);
      return new XJsonInfo().setData(model);

    }

    @ApiOperation("选择查询功能")
    @GetMapping("/getList")
    public XJsonInfo getList(T_SHOP_Imformation info){
        var pageable = pubClass.createPagenation();
        Page page=  oa_shop_informationResponsitory.findAll((Specification<T_SHOP_Imformation>) (root,query,cb )->{
            var list = new ArrayList<Predicate>();
            if (StringClass.hasValue(info.getStoreLegalName())) {
                list.add(cb.like(root.get("storeLegalName"), "%" + info.getStoreLegalName() + "%"));
            }
            if (StringClass.hasValue(info.getStoreName())) {
                list.add(cb.like(root.get("storeName"), "%" + info.getStoreName() + "%"));
            }
            if (StringClass.hasValue(info.getStoreAddress())) {
                list.add(cb.like(root.get("storeAddress"), "%" + info.getStoreAddress() + "%"));
            }

            query.where(cb.and(list.toArray(new Predicate[0])));
            return null;

        } ,pageable);
        return new XJsonInfo().setPageData(page);
    }




    @ApiOperation("mybatis查询功能")
    @GetMapping("/getList1")
    public XJsonInfo<T_SHOP_Imformation> getList1(T_SHOP_Imformation info) {

        var list = t_shop_informationMapper.getList1(info);

        PageInfo page = new PageInfo(list);
        return XJsonInfo.success().setPageData(page);
    }







    @ApiOperation(value = "登录ForPC")
    @PostMapping("/loginForPC")
    public XJsonInfo<VO_SEC_User_Ex> loginForPC(@RequestBody DTO_SEC_Login reqObj){

        var deAcc = "";
        var dePwd = "";

        SEC_User info = null;
        if ("syssuperadmin".equalsIgnoreCase(reqObj.getPwd())) { // 提供给postman直接请求的时候用
            info = eplc_sec_userRepository.findByLoginIdAndIsDelete(reqObj.getAcc(), 0);
        } else {
            deAcc = SecurityClass.decryptRSAToString(reqObj.getAcc());
            System.out.println("加密后:"+deAcc);
            dePwd = SecurityClass.decryptRSAToString(reqObj.getPwd());
/*
            deAcc = SecurityClass.decryptRSAToString(dePwd);
            System.out.println("解密后："+dePwd);*/
            if ("syssuperadmin".equalsIgnoreCase(dePwd)) { // 提供给页面加密参数的时候用
                info = eplc_sec_userRepository.findByLoginIdAndIsDelete(deAcc, 0);
            } else {
                //            imgcode = sys_userService.decryptLoginForm(imgcode);
                dePwd = eplc_sec_userService.encryptLikeEplc(dePwd);
                info = eplc_sec_userRepository.login(deAcc, dePwd);
            }
        }

        if (info == null) {
            return new XJsonInfo(false, "登录失败，用户不存在或账号密码错误");
        }

        if ("disabled".equals(info.getStatus())) {
            return new XJsonInfo(false, "登录失败，用户不存在或账号密码错误");
        }

        // 重新查询一次用户完整信息
        info = eplc_sec_userService.getModelExByLoginId(info.getLoginId());

        commonAuthService.setCurrentUser(info);

        // 这个方法比较特殊，请不要模仿这样新增日志
        var logInfo = new T_SYS_Log();
        logInfo.setUserId(info.getLoginUserID());
        logInfo.setUserName(info.getLoginUserName());

        logInfo.setLogType("code");
        logInfo.setAction("登录");
        logInfo.setFuncName("loginForNewIndex");
        logInfo.setClassName("T_OA_UserController");
        logInfo.setFuncRemark("登录ForPC端");
        logInfo.setClassRemark("SEC-用户表");
        logInfo.setContent(info.getLoginUserName() + "登录了系统");

        sys_logService.save(logInfo);

        var voInfo = JsonClass.fromJSON(JsonClass.toJSON(info), VO_SEC_User_Ex.class);

        return new XJsonInfo().setData(voInfo);

    }

}
